<template>
  <div>
    <el-dialog
      :title="title"
      :visible.sync="dialogVisible"
      width="600px"
      @closed="resetForm('form')"
      :close-on-click-modal="false"
    >
      <div>
        <el-form ref="form" :rules="rules" :model="form" label-width="120px">
          <el-form-item label="名称" prop="name">
            <el-input v-model.trim="form.name"></el-input>
          </el-form-item>
          <el-form-item label="编码" prop="code">
            <el-input v-model.trim="form.code"></el-input>
          </el-form-item>
          <el-form-item label="学期起始日期" prop="startDate" v-if="type == 1">
            <el-date-picker
              v-model="form.startDate"
              type="date"
              placeholder="选择日期"
              format="yyyy-MM-dd"
              value-format="yyyy-MM-dd"
              style="width: 100%"
              @change="handleChange"
            >
            </el-date-picker>
          </el-form-item>
          <el-form-item label="学期截止日期" prop="endDate" v-if="type == 1">
            <el-date-picker
              v-model="form.endDate"
              type="date"
              placeholder="选择日期"
              format="yyyy-MM-dd"
              value-format="yyyy-MM-dd"
              style="width: 100%"
              @change="handleChange"
            >
            </el-date-picker>
          </el-form-item>
          <el-form-item label="学期总周数" v-if="type == 1">
            <el-input v-model.trim="form.totalWeeks"></el-input>
          </el-form-item>
          <el-form-item label="起始周日期" v-if="type == 1">
            <el-date-picker
              v-model="form.startWeeksDate"
              type="date"
              placeholder="选择日期"
              format="yyyy-MM-dd"
              value-format="yyyy-MM-dd"
              style="width: 100%"
            >
            </el-date-picker>
          </el-form-item>
        </el-form>
      </div>
      <span slot="footer" class="dialog-footer">
        <el-button type="custom" v-preventReClick @click="submitForm('form')"
          >提 交</el-button
        >
        <el-button @click="dialogVisible = false">取 消</el-button>
      </span>
    </el-dialog>
  </div>
</template>
<script>
import { eduTierAdd, eduTierUpdate } from '@/api/admin/teach/eduTier.js'
import moment from 'moment'
import Bus from '@/utils/bus.js'
export default {
  data() {
    return {
      title: '新增',
      dialogVisible: false,
      addOrEdit: 1,
      checkedData: {},
      form: {
        code: '',
        name: '',
        type: this.type, //类型,1-学期，2-年级，3-班级
        schoolCode: '',
        startDate: undefined,
        endDate: undefined,
        totalWeeks: undefined,
        startWeeksDate: undefined,
      },
      rules: {
        name: [{ required: true, message: '请输入名称', trigger: 'blur' }],
        code: [{ required: true, message: '请输入编码', trigger: 'blur' }],
      },
    }
  },
  props: ['type'],
  mounted() {
    Bus.$on('showDialogClassroomAdd', (v) => {
      if (v) {
        this.checkedData = v
        this.handleAddOrEdit(2)
      } else {
        this.handleAddOrEdit(1)
      }
    })
  },
  beforeDestroy() {
    Bus.$off('showDialogClassroomAdd')
  },
  methods: {
    //显示弹窗
    handleAddOrEdit(type) {
      this.form = this.$options.data().form
      if (type == 2) {
        this.title = '编辑'
        for (let i in this.checkedData) {
          if (this.form.hasOwnProperty(i)) {
            this.form[i] = this.checkedData[i] || ''
          }
        }
        this.form.id = this.checkedData.id
      } else {
        this.title = '新增'
        delete this.form.id
      }
      this.addOrEdit = type
      this.dialogVisible = true
    },
    handleChange() {
      let { startDate, endDate } = this.form
      if (startDate && endDate) {
        const startTime = moment(startDate)
        const endTime = moment(endDate)
        this.form.totalWeeks = endTime.diff(startTime, 'weeks') + 1
      }
    },
    submitForm(form) {
      this.form.schoolCode = this.$store.getters.deptFullData.code
      this.$refs[form].validate((valid) => {
        if (valid) {
          this.addOrEdit == 1 ? this.add() : this.edit()
        }
      })
    },
    //新增
    add() {
      eduTierAdd(this.form).then((res) => {
        this.dialogVisible = false
        Bus.$emit('refreshClassroomList')
        this.$message({
          message: '操作成功',
          type: 'success',
        })
      })
    },
    //编辑
    edit() {
      eduTierUpdate(this.form).then((res) => {
        this.dialogVisible = false
        Bus.$emit('refreshClassroomList')
        this.$message({
          message: '操作成功',
          type: 'success',
        })
      })
    },
    //重置表单
    resetForm(form) {
      this.$refs[form].resetFields()
      this.form.name = ''
      this.form.code = ''
    },
  },
}
</script>